//
//  EFAPjtMgrResultsTVC.m
//  efairy
//
//  Created by XiangDongMBP on 2017/9/15.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFAPjtMgrResultsTVC.h"
#import "EFAProjectTCell.h"
#import <UIScrollView+EmptyDataSet.h>
#import <MJRefresh.h>
#import "EFAModel.h"
#import "GlobalHeader.h"
#import "ApiManager.h"
#import "EFAPjtDeviceAddVC.h"
#import "EFADeviceInfoVC.h"
#import "EFAMsgVC.h"
#import "EFAReportVC.h"
#import "EFADeviceVC.h"
#import "EFAPjtDetailVC.h"

@interface EFAPjtMgrResultsTVC () <DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>
@property (strong, nonatomic) NSMutableArray *searchPoiArray;
//@property (strong, nonatomic) MJRefreshNormalHeader *mj_header;
//@property (strong, nonatomic) MJRefreshAutoNormalFooter *mj_footer;
@property (nonatomic, strong) EFAProjectModel *projectModel;

@end

static NSString *const kReuseCellId = @"EFAProjectTCell";
//static NSInteger const kRequestSize = 20;

@implementation EFAPjtMgrResultsTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubViews];
}
- (void)initSubViews {
    UINib *nib = [UINib nibWithNibName:kReuseCellId bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:kReuseCellId];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 67.0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    // 下拉刷新
    //    self.tableView.mj_header = self.mj_header;
    //    self.tableView.mj_footer = self.mj_footer;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - DZNEmptyDataSetSource
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:14.0]};
    return [[NSAttributedString alloc] initWithString:@"没有搜到相关项目" attributes:dic];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    //    return [KABDefaultVarModel sharedInstance].bitmapDefaultImg;
    return nil;
}

//- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
//    return -100.0;
//}
#pragma mark - DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

#pragma mark - UITableViewDataSource
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return 1;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.projectModel.project_list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EFAProjectTCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseCellId forIndexPath:indexPath];
    if (indexPath.row < self.projectModel.project_list.count) {
        [cell configViewWithPjtItem:self.projectModel.project_list[indexPath.row]];
    }
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 14.9;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < self.projectModel.project_list.count) {
        [self jumpToSomeVCWithProjectItem:self.projectModel.project_list[indexPath.row]];
    }
}

#pragma mark - event responds
- (void)refreshNewData {
    [self requestPjtListWithHeader:YES];
}

#pragma mark - private methods
- (void)jumpToSomeVCWithProjectItem:(EFAPjtListItem *)item {
    if (self.viewType == EFAPjtManagerVCTypeMsg) {
        [self jumpToMsgVCWithProjectItem:item];
    } else if(self.viewType == EFAPjtManagerVCTypeDevice) {
        [self jumpToDeviceWithProjectItem:item];
    } else if(self.viewType == EFAPjtManagerVCTypeManager) {
        [self jumpToPjtDetailWithPjtItem:item];
    } else if (self.viewType == EFAPjtManagerVCTypeReport) {
        [self jumpToReportWithPjtItem:item];
    }
}
- (void)jumpToMsgVCWithProjectItem:(EFAPjtListItem *)item {
    EFAMsgVC *vc = [[EFAMsgVC alloc] init];
    vc.project_id = item.efairyproject_id;
    [self.presentingViewController showViewController:vc sender:nil];
}
- (void)jumpToDeviceWithProjectItem:(EFAPjtListItem *)item {
    EFADeviceVC *vc = [[EFADeviceVC alloc] init];
    vc.project_id = item.efairyproject_id;
    vc.project_name = item.efairyproject_name;
    [self.presentingViewController showViewController:vc sender:nil];
}
- (void)jumpToPjtDetailWithPjtItem:(EFAPjtListItem *)item {
    EFAPjtDetailVC *vc = [[EFAPjtDetailVC alloc] init];
    vc.project_id = item.efairyproject_id;
    [self.presentingViewController showViewController:vc sender:nil];
}
- (void)jumpToReportWithPjtItem:(EFAPjtListItem *)item {
    EFAReportVC *vc = [[EFAReportVC alloc] init];
    vc.project_id = item.efairyproject_id;
    vc.project_name = item.efairyproject_name;
    [self.presentingViewController showViewController:vc sender:nil];
}

- (void)refreshWholeView {
    //    // 尾部加载更多的显示
    //    if (self.userList.totalLoad) {
    //        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    //    } else {
    //        [self.tableView.mj_footer resetNoMoreData];
    //    }
    
    [self.tableView reloadData];
}

- (void)tableViewEndRefresh:(BOOL)isHeader {
    NSLog(@"停止刷新是否是头部---%zd", isHeader);
    if (isHeader) {
        [self.tableView.mj_header endRefreshing];
    } else {
        [self.tableView.mj_footer endRefreshing];
    }
}

- (void)tableViewRefresh:(id)sender {
    BOOL isHeader = [sender isKindOfClass:[MJRefreshHeader class]];
    [self requestPjtListWithHeader:isHeader];
}

#pragma mark - network request
- (void)requestPjtListWithHeader:(BOOL)isHeader {
    // 发布Cos需要登录
    if (![EFAModel sharedInstance].isLogin) {
        [self tableViewEndRefresh:isHeader];
        return;
    }
    
    NSDictionary *pars = @{@"efairyuser_id" : @([EFAModel sharedInstance].loginInfo.user_info.efairyuser_id),
                           @"access_token" : [EFAModel sharedInstance].loginInfo.access_token,
                           @"keyword" : (self.keywords ? self.keywords : @""),
                           @"manager_type" : @(self.roleType)};
    
    NSString *url = (self.viewType == EFAPjtManagerVCTypeManager) ? [ApiManager apiPjtManagerList] : [ApiManager apiProjectList];
    [[XDAFNetworking sharedInstance] GET:url parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求项目列表返回：%@", responseObject);
        [self tableViewEndRefresh:isHeader];
        if (self.viewType != EFAPjtManagerVCTypeManager) {
            self.projectModel = [[EFAModel sharedInstance] projectModelWithDictionary:responseObject];
        } else {
            self.projectModel = [EFAProjectModel yy_modelWithDictionary:responseObject[EFAGlobalJsonKeyResult]];
        }
        [self refreshWholeView];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求项目列表失败：%@", error);
        [self tableViewEndRefresh:isHeader];
        NSString *msg = [[ApiErrorCode sharedInstance] errorCodeMsgWithError:error];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:msg];
    }];
}
#pragma mark - gettes and setters
//- (MJRefreshNormalHeader *)mj_header {
//    if (!_mj_header) {
//        _mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(tableViewRefresh:)];
//        _mj_header.lastUpdatedTimeLabel.hidden = YES;
//    }
//    return _mj_header;
//}
//- (MJRefreshAutoNormalFooter *)mj_footer {
//    if (!_mj_footer) {
//        _mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(tableViewRefresh:)];
//        _mj_footer.automaticallyRefresh = NO;
//        _mj_footer.automaticallyHidden = YES;
//    }
//    return _mj_footer;
//}

@end
